// const T_paper_row = require('../models/t_exam_paper_info_row')
const { T_exam_target_to_question } = require('../models/t_exam_target_to_question')
const { T_exam_target } = require('../models/t_exam_target')

const { Op } = require('sequelize')
const { T_exam_label } = require('../models/t_exam_label')
const { Class_Content } = require('../models/Course outline/class_Content')
const { t_label_to_realLabel } = require('../models/T_label_to_realLabel')
const { T_exam_question } = require('../models/t_exam_question')
const { T_exam_questions_and_label } = require('../models/t_exam_questions_and_label')


exports.addOne = async (req, res) => {
    const body = req.body


    try {
        if (body.ACSId && body.ACSId != null) {
            T_exam_target_to_question.create({
                targetId: body.targetId,
                paperType: body.paperType,
                t_user_and_a_c_s_id: body.ACSId,
            })
            return res.json({
                "code": 200,
                "content": body
            })
        } else {
            return res.json({
                "code": 500,
                "content": 未传正确的值
            })
        }



    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        })
    }

}


exports.deleteOne = async (req, res) => {
    const id = req.query.id


    try {
        T_exam_target_to_question.destroy({
            where: {
                id: id
            }
        })
        res.json({
            "code": 200,
            "content": "删除成功"
        })
    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        })
    }

}

exports.quickFind = async (req, res) => {
    try {
        const ACSId = req.query.ACSId
        const subId = req.query.subId
        const paperType = req.query.paperType
        const mes = await T_exam_target_to_question.findAll({
            where: {
                t_user_and_a_c_s_id: ACSId,
                paperType: paperType,
            },
            include: [{
                model: T_exam_target, // 关联的模型
                attributes: ['target_name'], // 需要查询的属性
                as: 'target'
            }]
        });

        // console.log(mes.length);
        if (mes.length !== 0) {
            for (let i = 0; i < mes.length; i++) {
                // console.log("ajajjajaja  i=", i);
                const data = await Class_Content.findAll({
                    where: {
                        t_user_and_a_c_s_id: ACSId,
                        target_id: mes[i].targetId
                    },
                    attributes: ['tec_content',],

                })
                // console.log(data);

                const label = await t_label_to_realLabel.findAll({
                    where: {
                        target_id: mes[i].targetId,
                        paper_type: paperType
                    },
                    attributes: ['id'],
                    include: [{
                        model: T_exam_label, // 关联的模型
                        attributes: ['label'], // 需要查询的属性
                        as: 'label'
                    }]
                })

                const newlabel = label.map(obj => {
                    if (obj.label) {
                        const labelValue = obj.label.label;
                        return {
                            id: obj.id,
                            value: labelValue,
                            label: labelValue,
                        };
                    } else {
                        return {
                            id: obj.id
                        }
                    }


                })


                const array = []
                if (data.length != 0) {
                    // console.log(data);

                    data.forEach(item => {
                        array.push(item.tec_content)
                    })

                    // console.log("第好东哥", array);
                    // console.log("第好东哥", data[0].target.target_name)
                    mes[i].dataValues.target_desc = array;
                    mes[i].dataValues.label = newlabel;
                    mes[i].dataValues.target_name = mes[i].dataValues.target.target_name;
                    // console.log("我是么事 = ", mes);
                } else {
                    mes[i].dataValues.target_desc = array;
                    mes[i].dataValues.label = label;
                    mes[i].dataValues.target_name = mes[i].dataValues.target.target_name;
                }

            }


            return res.json({
                "code": 200,
                "content": mes
            });
        } else {
            return res.json({
                "code": 200,
                "content": '无数据'
            });
        }
    } catch (error) {
        res.json({
            "code": 500,
            "content": error.msg
        });
    }
};


exports.getQuestionLabel = async (req, res) => {
    try {
        const subId = req.query.subId
        const mes = await T_exam_label.findAll({
            where: {
                sub_id: subId
            },
            attributes: ['id', 'label']
        })
        return res.json({
            "code": 200,
            "content": mes
        });
    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        });
    }

}


exports.postQuestionLabel = async (req, res) => {
    try {
        const targetId = req.body.targetId
        const labelId = req.body.labelId
        const paperType = req.body.paperType

        console.log("target = ", targetId, "labelId", labelId, paperType);
        if (targetId && labelId && paperType) {
            const mes = await t_label_to_realLabel.findOne({
                where: {
                    label_id: labelId,
                    target_id: targetId,
                    paper_type: paperType
                }

            })
            // console.log(mes);
            if (!mes) {
                t_label_to_realLabel.create({
                    label_id: labelId,
                    target_id: targetId,
                    paper_type: paperType
                })

                return res.json({
                    "code": 200,
                    "content": "插入成功"
                });
            } else {
                return res.json({
                    "code": 200,
                    "content": "重复插入"
                });
            }

        } else {
            return res.json({
                "code": 200,
                "content": "缺少信息"
            });
        }


    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        });
    }
}


exports.deleteQuestionLabel = async (req, res) => {
    try {
        const targetId = req.query.targetId
        const labelId = req.query.labelId
        const paperType = req.query.paperType
        let mes
        if (targetId && labelId) {
            mes = await t_label_to_realLabel.findOne({
                where: {
                    paper_type: paperType,
                    target_id: targetId,
                    label_id: labelId,
                }

            })
        }

        // console.log(mes);
        if (mes) {
            t_label_to_realLabel.destroy({
                where: {
                    label_id: labelId,
                    target_id: targetId
                }

            })
            return res.json({
                "code": 200,
                "content": "删除成功"
            });
        } else {
            return res.json({
                "code": 200,
                "content": "无数据删除"
            });
        }

    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        });
    }



}

exports.quickUpdate = async (req, res) => {
    const body = req.body
    const data = body.data
    try {
        for (let i = 0; i < data.length; i++) {

            await T_exam_target_to_question.update({
                questionType: data[i].questionType,
                questionNum: data[i].questionNum,
                score: data[i].score,

            }, {
                where: {
                    id: data[i].id
                }
            }
            );

        }

        res.json({
            "code": 200,
            "content": "成功"
        })
    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        })
    }
}

exports.quickfillALL = async (req, res) => {



    const body = req.body
    const data = body.data
    const paperType = body.paperType
    const ACSId = body.ACSIdId
    try {
        for (let i = 0; i < data.length; i++) {

            await T_exam_target_to_question.create({
                questionType: data[i].type,
                questionNum: data[i].questionNum,
                targetId: data[i].targetId,
                score: data[i].score,

                paperType: paperType,
                t_user_and_a_c_s_id: ACSId
            });

            // res.json({
            //     "code": 1,
            //     "msg": '创建成功',
            //     "data": T_info,
            // });
        }

        res.json({
            "code": 200,
            "content": body
        })
    } catch (error) {
        res.json({
            "code": 500,
            "content": error
        })
    }
}